<?php
namespace App\Http\Controllers\Web\User;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use App\Http\Controllers\Web\Controller;
use TwOauth\Tongwei;
use TwOauth\TwSdk;
use TwOauth\Config;
use App\Services\Base\UserService;
use App\Http\Resources\Base\UserResource;
use App\Core\Entity\Cookie\UserEntity as CookieUserEntity;

class IndexController extends Controller
{

    public function index()
    {
        return redirect()->route('user.signIn');
    }

    /**
     * 注册
     *
     * @return \Illuminate\Routing\Redirector
     */
    public function signUp()
    {
        return redirect(Config::getSignUp());
    }

    /**
     * 登录
     *
     * @param Request $request            
     * @return \Illuminate\Routing\Redirector
     */
    public function signIn(Request $request)
    {
        // 缓存 redirect 页面
        $redirect_uri = $request->query('redirect_uri');
        if (! $redirect_uri) {
            $redirect_uri = $this->identity()->getRedirectUri();
        }
        $this->identity()
            ->setRedirectUri($redirect_uri)
            ->save();
        
        // 登录跳转
        $callback_uri = route('user.oauthCallback');
        $tongwei = new Tongwei($callback_uri);
        return redirect($tongwei->login());
    }

    /**
     * 退出
     *
     * @param Request $request            
     * @return \Illuminate\Routing\Redirector
     */
    public function signOut(Request $request)
    {
        // 读取 redirect 链接
        $redirect_uri = $request->query('redirect_uri');
        if (! $redirect_uri) {
            $redirect_uri = $this->identity()->getRedirectUri();
        }
        
        // 删除 session 内用户数据
        $this->identity()
            ->initData()
            ->save();
        
        // 保存数据到 cookie
        (new CookieUserEntity($this->identity()))->save();
        
        // redirect 页面跳转
        return redirect($redirect_uri);
    }

    /**
     * Oauth回调
     *
     * @param Request $request            
     * @return \Illuminate\Routing\Redirector
     */
    public function oauthCallback()
    {
        // 获取 redirect 缓存
        $redirect_uri = $this->identity()->getRedirectUri();
        
        // 处理登录后续数据
        $callback_uri = route('user.oauthCallback');
        $tongwei = new tongwei($callback_uri);
        $access_token = $tongwei->callBack();
        
        if (! $access_token) {
            abort(404);
        }
        $sdk = new TwSdk($access_token, $callback_uri);
        $data = $sdk->get_user_info();
        
        // 查询数据库数据
        $userService = new UserService();
        $user = UserResource::makeDataDecode($userService->findSignUser($data['id']));
        
        // 保存数据到 session
        $this->identity()
            ->initData($user)
            ->save();
        
        // 保存数据到 cookie
        (new CookieUserEntity($this->identity()))->save();
        
        // 清空 sdk 缓存参数
        $sdk->invalidate();
        
        // redirect 页面跳转
        return redirect($redirect_uri);
    }
}